/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package bombercloud;

import java.awt.Color;
import java.awt.Dimension;
import java.awt.HeadlessException;
import java.awt.Rectangle;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;
import javax.swing.JFrame;
import javax.swing.JPanel;

/**
 *
 * @author Leonardo Oliveira Moreira
 */
public class Scenario extends JFrame {

    public static final int MAX_FIVE_PLAYERS = 5;
    public static final int MAX_TEN_PLAYERS = 10;
    public static final int MAX_ONE_HUNDRED_PLAYERS = 100;
    public static final int MAX_FIVE_HUNDRED_PLAYERS = 500;
    public static final int MAX_ONE_MILLION_PLAYERS = 1000;

    private Dimension sizeOfAvatar;
    private Dimension sizeOfBlock;

    private List<Player> playersList;
    private List<Block> blocksList;

    private JPanel scenario;

    public Scenario() throws HeadlessException {
        playersList = new ArrayList<Player>();
        blocksList = new ArrayList<Block>();
    }

    public void build(int maximumNumberOfPlayers) {
        setTitle("BomberCloud");
        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setResizable(false);
        scenario = new JPanel(null);
        getContentPane().setBackground(Color.BLACK);
        getContentPane().setLayout(null);
        scenario.setBackground(Color.decode("#228B22"));
        switch (maximumNumberOfPlayers) {
            case MAX_FIVE_PLAYERS: {
                scenario.setSize(800, 600);
                sizeOfAvatar = new Dimension(50, 50);
                sizeOfBlock = new Dimension(50, 50);
                break;
            }
            case MAX_TEN_PLAYERS: {
                scenario.setSize(800, 600);
                sizeOfAvatar = new Dimension(50, 50);
                sizeOfBlock = new Dimension(50, 50);
                break;
            }
            case MAX_ONE_HUNDRED_PLAYERS: {
                scenario.setSize(1200, 800);
                sizeOfAvatar = new Dimension(50, 50);
                sizeOfBlock = new Dimension(50, 50);
                break;
            }
            case MAX_FIVE_HUNDRED_PLAYERS: {
                scenario.setSize(1000, 800);
                sizeOfAvatar = new Dimension(50, 50);
                sizeOfBlock = new Dimension(50, 50);
                break;
            }
            case MAX_ONE_MILLION_PLAYERS: {
                scenario.setSize(1000, 800);
                sizeOfAvatar = new Dimension(50, 50);
                sizeOfBlock = new Dimension(50, 50);
                break;
            }
            default: {

            }
        }
        buildBlocks();
        setSize(scenario.getWidth() + 50, scenario.getHeight() + 50);
        int x = ((this.getWidth() / 2) - scenario.getWidth() / 2) - 2;
        int y = ((this.getHeight() / 2) - scenario.getHeight() / 2) - 4;
        scenario.setLocation(x, y);
        getContentPane().add(scenario);
    }

    private void buildBlocks() {
        if (scenario.getWidth() == 800 && scenario.getHeight() == 600) {
            for (int y = 100; y < scenario.getHeight() - sizeOfAvatar.height; y += 150) {
                for (int x = 150; x < scenario.getWidth() - sizeOfAvatar.width; x += 150) {
                    Block block = new Block(this, x, y);
                    block.getBlock().setLocation(x, y);
                    blocksList.add(block);
                    scenario.add(block.getBlock());
                    scenario.repaint();
                }
            }
            return;
        }
        if (scenario.getWidth() == 1000 && scenario.getHeight() == 800) {
            for (int y = 150; y < scenario.getHeight() - sizeOfAvatar.height; y += 150) {
                for (int x = 100; x < scenario.getWidth() - sizeOfAvatar.width; x += 150) {
                    Block block = new Block(this, x, y);
                    block.getBlock().setLocation(x, y);
                    blocksList.add(block);
                    scenario.add(block.getBlock());
                    scenario.repaint();
                }
            }
            return;
        }
    }

    public void execute() {
        setTitle("BomberCloud - Players Connected: " + playersList.size());
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public synchronized void addPlayer(Player player) {
        Random random = new Random();
        List<Integer> positionX = new ArrayList<Integer>();
        List<Integer> positionY = new ArrayList<Integer>();
        for (int x = 0; x <= scenario.getWidth() - sizeOfAvatar.width; x += sizeOfAvatar.width) {
            positionX.add(x);
        }
        for (int y = 0; y <= scenario.getHeight() - sizeOfAvatar.height; y += sizeOfAvatar.height) {
            positionY.add(y);
        }
        boolean foundPosition = false;
        while (!foundPosition) {
            int x = positionX.get(random.nextInt(positionX.size()));
            int y = positionY.get(random.nextInt(positionY.size()));
            player.setX(x);
            player.setY(y);
            if (isCollision(player, x, y)) {
                continue;
            } else {
                foundPosition = true;
            }
        }
        playersList.add(player);
    }

    public synchronized void updatePlayerPosition(Player player) {
        try {
            JPanel avatar = player.getAvatar();
            scenario.remove(avatar);
            scenario.add(avatar);
            scenario.repaint();
        } catch (java.lang.Exception ex) {
        }
    }

    public synchronized boolean isCollision(Player player, int x, int y) {
        for (Block b : blocksList) {
            if (b.getBlock().getBounds().intersects(new Rectangle(x, y, player.getAvatarWidth(), player.getAvatarHeight()))) {
                return true;
            }
        }
        for (Player p : playersList) {
            if (p != player) {
                if (p.getAvatar().getBounds().intersects(new Rectangle(x, y, player.getAvatarWidth(), player.getAvatarHeight()))) {
                    return true;
                }
            }
        }
        return false;
    }

    public Dimension getSizeOfAvatar() {
        return sizeOfAvatar;
    }

    public Dimension getSizeOfBlock() {
        return sizeOfBlock;
    }

    public int getScenarioWidth() {
        return scenario.getWidth();
    }

    public int getScenarioHeight() {
        return scenario.getHeight();
    }

}
